package jllowrey.controller;

import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import jllowrey.model.DecisionLinesEvent;
import jllowrey.model.Edge;
import jllowrey.view.Application;
import jllowrey.view.DrawingPanel;

/**
 * DraweEdgeController
 * 
 * @author J Lowrey
 *
 */
public class DrawEdgeController extends MouseAdapter {

	Application view;
	DecisionLinesEvent event;

	/**
	 * Constructor, draws an edge using the y-value of an ActionEvent(ae), creates an edge to be added to the decisionLinesEvent, if it is valid
	 * 
	 * @param ae, an action performed 
	 */
	public DrawEdgeController(DecisionLinesEvent dle, Application app){
		this.view = app;
		this.event = dle;



	}

	/**
	 *on a mouse event generate its coordinates
	 * 
	 * @param me, a mouse event
	 */
	public void mousePressed(MouseEvent me){
		process (me.getX(), me.getY());
	}	
	
	/**
	 * Process registers an addEdge and adds it
	 * 
	 * @param xPos
	 * @param yPos
	 */
	public void process(int xPos, int yPos) {

		int width = (view.getWidth()/event.getNumChoices());
	
		if (event.isDone() == false) {
			if (xPos < width/2 | xPos > view.getWidth()-width){
				System.out.println("out of bounds, not valid");
			}
			else{

				for (int i=0; i < event.getNumChoices(); i++){
					if (width*i < xPos && xPos < width*(i+1)){
						Edge e = new Edge(xPos/width, (xPos/width)+1, yPos);
						if (event.addEdge(e)){
							System.out.println("edge added");
						}
						else{
							System.out.println("Edge was not added");
						}
					}
					else{
						//System.out.println("not valid");
					}
				}
			}

			view.getDrawingPanel().repaint();
		}
		System.out.print("I Dont have the results");


	}
}